package SocialMediaSentiment;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;


public class bounderyCalculator {

	public static float[] calcBoundery(HashMap<Integer, Float[]> tweetCalcValRealVal, float previousBoundery) throws IOException{
		float[] bounderyerror = new float[2];
		float besterror = 100;         
		float stepsize = 0.01f;
		float bestBoundery = previousBoundery;
		float uppererror, lowererror;

		besterror = calcerror(tweetCalcValRealVal, previousBoundery);
		while(stepsize < 1)
		{
			uppererror = calcerror(tweetCalcValRealVal, bestBoundery + stepsize);
			lowererror = calcerror(tweetCalcValRealVal, bestBoundery - stepsize);
			if( uppererror < besterror && uppererror <= lowererror )
			{
				bestBoundery = bestBoundery + stepsize;
				besterror = uppererror;
				stepsize = 0.01f;
			}
			else if( lowererror < besterror && lowererror < uppererror )
			{
				bestBoundery = bestBoundery - stepsize;
				besterror = lowererror;
				stepsize = 0.01f;
			}
			else 
				stepsize = stepsize * 2;
		}
		bounderyerror[0] = bestBoundery;
		bounderyerror[1] = besterror;
		return bounderyerror;
		     

	}

	private static float calcerror(
			HashMap<Integer, Float[]> tweetCalcValRealVal,
			float previousBoundery) {
		Iterator<Integer> k = (Iterator<Integer>) tweetCalcValRealVal.keySet().iterator();
		int fouten = 0;
		int total = 0;
		while(k.hasNext())
		{
			int index = k.next();
			Float[] CalcValRealVal = tweetCalcValRealVal.get(index);
			//S/ystem.out.println(CalcValRealVal[0]);
			//classificatie boven boundery is sentiment, maar real value is geen sentiment -> dan fout
			if( CalcValRealVal[0]>=previousBoundery && CalcValRealVal[1] == 0)
			{
				fouten++;
			}
			else if( CalcValRealVal[0] < previousBoundery && CalcValRealVal[1] == 1)
			{
				fouten++;
			}
			total++;
		}
		System.out.println(fouten+" "+total);
		return (float)fouten/total;
	}
	
}
